package com.example.depcontrol.controller;

import com.example.depcontrol.anno.Log;
import com.example.depcontrol.pojo.Emp;
import com.example.depcontrol.pojo.PageBean;
import com.example.depcontrol.pojo.Result;
import com.example.depcontrol.service.EmpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
public class EmpController {

    @Resource
    private EmpService empService;

    //条件查询
    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
                       ) {

        log.info("分页查询， 参数：{}, {}, {}, {}, {}, {}", page, pageSize, name, gender, begin, end);
        PageBean pageBean=empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }

    //批量删除
    @Log
    @DeleteMapping("emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids)
    {
        log.info("批量删除, ids{}", ids);
        empService.deleteById(ids);
        return Result.success();
    }

    //添加员工
    @Log
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp)
    {
        log.info("添加员工,{}", emp);
        empService.addEmp(emp);
        return Result.success();
    }

    //根据ID查询
    @GetMapping("/emps/{id}")
    public Result query(@PathVariable Integer id)
    {
        log.info("根据ID查询：{}", id);
        Emp res = empService.getById(id);
        return Result.success(res);
    }

    //修改员工
    @Log
    @PutMapping("/emps")
    public Result updateEmp(@RequestBody Emp emp)
    {
        log.info("修改员工：{}", emp);
        empService.updateById(emp);
        return Result.success();
    }


}
